<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>我的任务申请</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div th:replace="~{fragments/header :: header}"></div>
    
    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>我的任务申请</h2>
        </div>
        
        <!-- 搜索表单 -->
        <div class="card mb-4">
            <div class="card-body">
                <form th:action="@{/freelancer/task-applications}" method="get" class="row g-3">
                    <div class="col-md-10">
                        <label for="status" class="form-label">承接状态</label>
                        <select class="form-control" id="status" name="status">
                            <option value="">全部状态</option>
                            <option value="not_delivered" th:selected="${searchStatus == 'not_delivered'}">未交付</option>
                            <option value="delivered" th:selected="${searchStatus == 'delivered'}">已交付</option>
                            <option value="confirmed" th:selected="${searchStatus == 'confirmed'}">已确认</option>
                        </select>
                    </div>
                    <div class="col-md-2 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary me-2">筛选</button>
                        <a th:href="@{/freelancer/task-applications}" class="btn btn-secondary">重置</a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 任务承接列表 -->
        <div class="row">
            <div class="col-12">
                <div th:if="${applications.empty}" class="alert alert-info">
                    您还没有承接任何任务
                </div>
                
                <div th:if="${!applications.empty}">
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>承接编号</th>
                                    <th>任务编号</th>
                                    <th>任务名称</th>
                                    <th>承接时间</th>
                                    <th>交付时间</th>
                                    <th>交付状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="app : ${applications}">
                                    <td th:text="${app.applicationId}">承接编号</td>
                                    <td th:text="${app.taskId}">任务编号</td>
                                    <td>
                                        <span th:text="${taskMap.get(app.applicationId)?.title ?: '未知任务'}">
                                            任务名称
                                        </span>
                                    </td>
                                    <td th:text="${#temporals.format(app.applyTime, 'yyyy-MM-dd HH:mm')}">承接时间</td>
                                    <td th:text="${#temporals.format(app.deliveryTime, 'yyyy-MM-dd HH:mm')}">交付时间</td>
                                    <td>
                                        <span th:if="${app.deliveryStatus == 'not_delivered'}" class="badge bg-primary">未交付</span>
                                        <span th:if="${app.deliveryStatus == 'delivered'}" class="badge bg-warning">已交付</span>
                                        <span th:if="${app.deliveryStatus == 'confirmed'}" class="badge bg-success">已确认</span>
                                    </td>
                                    <td>
                                        <a th:href="@{/freelancer/task-applications/{id}/submit-delivery(id=${app.applicationId})}" class="btn btn-sm btn-success" th:if="${app.deliveryStatus == 'not_delivered'}">提交成果物</a>
                                        <form th:action="@{/freelancer/task-applications/{id}/retract(id=${app.applicationId})}" method="post" style="display: inline;" th:if="${app.deliveryStatus == 'delivered'}">
                                            <button type="submit" class="btn btn-sm btn-warning"
                                                    onclick="return confirm('确定要撤回交付吗？')">撤回交付</button>
                                        </form>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div th:replace="~{fragments/footer :: footer}"></div>
</body>
</html>